// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   XmlReader.java

package com.vandagroup.common.util;

import java.io.File;
import java.io.InputStream;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XmlReader
{

    public static int USE_FILE = 1;
    public static int USE_CLASSLOADER = 2;
    public static int USE_INPUTSTREAM = 3;
    private int currUseType;
    private Document dom;
    private String filePath;
    private String classPath;
    private InputStream inputStream;

    public Document getDom()
    {
        return dom;
    }

    public String getFilePath()
    {
        return filePath;
    }

    public InputStream getInputStream()
    {
        return inputStream;
    }

    public void setDom(Document dom)
    {
        this.dom = dom;
    }

    public void setFilePath(String filePath)
    {
        this.filePath = filePath;
    }

    public void setInputStream(InputStream inputStream)
    {
        this.inputStream = inputStream;
    }

    public String getClassPath()
    {
        return classPath;
    }

    public void setClassPath(String classPath)
    {
        this.classPath = classPath;
    }

    public int getCurrUseType()
    {
        return currUseType;
    }

    public void setCurrUseType(int currUseType)
    {
        this.currUseType = currUseType;
    }

    public XmlReader()
    {
        currUseType = USE_FILE;
        filePath = null;
        classPath = null;
        inputStream = null;
    }

    public XmlReader(String path, int useType)
        throws Exception
    {
        currUseType = USE_FILE;
        filePath = null;
        classPath = null;
        inputStream = null;
        if(useType == USE_FILE)
        {
            setFilePath(path);
            setCurrUseType(useType);
        } else
        if(useType == USE_CLASSLOADER)
        {
            setClassPath(path);
            setCurrUseType(useType);
        }
        parse();
    }

    public XmlReader(InputStream inputStream)
        throws Exception
    {
        currUseType = USE_FILE;
        filePath = null;
        classPath = null;
        this.inputStream = null;
        setInputStream(inputStream);
        setCurrUseType(USE_INPUTSTREAM);
        parse();
    }

    public void parse()
        throws Exception
    {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try
        {
            builder = docBuilderFactory.newDocumentBuilder();
        }
        catch(ParserConfigurationException ex)
        {
            ex.printStackTrace();
        }
        if(currUseType == USE_FILE)
        {
            File file = null;
            file = new File(filePath);
            dom = builder.parse(file);
        }
        if(currUseType == USE_CLASSLOADER)
        {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            if(classLoader == null)
                classLoader = getClass().getClassLoader();
            inputStream = classLoader.getResourceAsStream(classPath);
            dom = builder.parse(inputStream);
        }
        if(currUseType == USE_INPUTSTREAM)
            dom = builder.parse(inputStream);
    }

    public Element[] getElements(Element parent, String tagName)
    {
        Element element = parent;
        NodeList children = element.getChildNodes();
        Vector found = new Vector();
        for(int i = 0; i < children.getLength(); i++)
        {
            Node nodei = children.item(i);
            int type = nodei.getNodeType();
            if((nodei instanceof Element) && ((Element)nodei).getTagName().equalsIgnoreCase(tagName))
                found.addElement(nodei);
        }

        if(found.size() > 0)
        {
            Element rvs[] = new Element[found.size()];
            found.copyInto(rvs);
            return rvs;
        } else
        {
            return new Element[0];
        }
    }

}
